package com.zmzncs.lmtc.module.customer.service.impl;

import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.enums.WhetherEnum;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.customer.CarTypeEnum;
import com.zmzncs.lmtc.common.util.BolianUtil;
import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.module.customer.dao.CardDao;
import com.zmzncs.lmtc.module.customer.dao.CustomerDao;
import com.zmzncs.lmtc.module.customer.pojo.car.Car;
import com.zmzncs.lmtc.module.customer.dao.CarDao;
import com.zmzncs.lmtc.module.customer.pojo.car.CarDTO;
import com.zmzncs.lmtc.module.customer.pojo.card.CardDTO;
import com.zmzncs.lmtc.module.customer.pojo.customer.Customer;
import com.zmzncs.lmtc.module.customer.pojo.customer.CustomerDTO;
import com.zmzncs.lmtc.module.customer.service.CarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.module.customer.service.CardService;
import com.zmzncs.lmtc.module.customer.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 客户-车辆 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-08-31
 */
@Slf4j
@Service
public class CarServiceImpl extends ServiceImpl<CarDao, Car> implements CarService {

    @Autowired
    CarDao carDao;
    @Autowired
    CustomerDao customerDao;
    @Autowired
    CardDao cardDao;
    @Autowired
    CustomerService customerService;
    @Autowired
    CardService cardService;
    @Autowired
    HttpServletRequest request;

    /**
     * 获取客户车辆
     */
    @Override
    public List<CarDTO> getCustomerCarList() {
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        List<CarDTO> customerCarList = carDao.getCustomerCarList(loginUser.getCustomerId());
        return customerCarList;
    }

    /**
     * 根据车牌号获取车辆
     */
    @Override
    public CarDTO getCarByCarNumber(String carNumber) {
        CarDTO carDTO = carDao.getCarByCarNumber(carNumber);
        return carDTO;
    }

    /**
     * 添加客户车牌
     */
    @Override
    @Transactional
    public void addCustomerCar(String carNumber) {
        CarDTO carDTO = carDao.getCustCarByCarNumber(carNumber);
        if(carDTO != null){
            ExceptionUtil.businessException("车牌号已绑定");
        }
        //  获取客户
        CustomerDTO customerInfo = customerService.getCustomerInfo();
        if(customerInfo == null){
            ExceptionUtil.businessException("无此用户");
        }

        //  添加车牌
        Car car = new Car();
        car.setCustomerId(customerInfo.getId());
        car.setCarNumber(carNumber);
        car.setCarType(carNumber.length() == 5 ? CarTypeEnum.FUEL.getCode() : CarTypeEnum.NEW_ENERGY.getCode());
        carDao.insert(car);

        //  上传会员和车牌到泊链
        cardService.resetUserAndCarToBoLian(customerInfo.getId());
    }

    /**
     * 删除客户车牌
     */
    @Override
    @Transactional
    public void deleteCustomerCar(Long id) {
        //  获取车牌未到期的月卡
        List<CardDTO> carCardList = cardDao.getCarUnexpiredCardList(id);
        if(carCardList.size() > 0){
            ExceptionUtil.businessException("该车牌号有未到期的月卡，不能删除");
        }

        //  获取登录客户
        LoginUser loginUser = JwtUtil.getLoginUser(request);

        //  删除客户车牌
        Car car = new Car();
        car.setId(id);
        car.setIsDeleted(WhetherEnum.YES.getCode());
        CustomerDTO customerDTO = customerDao.getCustomerById(loginUser.getCustomerId());
        //  删除泊链会员车牌
        Car carById = carDao.selectById(id);
        Boolean falg = BolianUtil.deleteUserCarNumberFromBoLian(customerDTO.getId(), carById.getCarNumber());
        if(!falg){
            ExceptionUtil.businessException("泊链删除车牌失败");
        }

        //  是否上传到泊链（1：是, 0：否）
        car.setIsToBolian(WhetherEnum.NO.getCode());
        carDao.updateById(car);

        //  如果客户没有绑定的车牌了，泊链就会删除客户
        List<CarDTO> carList = carDao.getCustomerCarList(loginUser.getCustomerId());
        if(carList.size()==0){
            Customer customer = new Customer();
            customer.setId(loginUser.getCustomerId());
            customer.setIsToBolian(WhetherEnum.NO.getCode());
            customerDao.updateById(customer);
        }
    }

}
